﻿using System.Collections.Generic;
using Common;
using Microsoft.Xna.Framework;

namespace Domain.Map
{
    /// <summary>
    /// 地图单元
    /// </summary>
    public class MapUnit
    {
        /// <summary>
        /// 地图切分成最小单元的所有集合300X300个(一张地图图片可分割成10*10个小单元)，存储落在单元格中的建筑、军队等游戏元素
        /// </summary>
        public static Dictionary<string, MapUnit> MapUnits;
        private static List<MapUnit> _displayMapUnits = new List<MapUnit>();//当前显示中的地图单元集合

        public MapUnit(int column, int row)
        {
            this.Column = column;
            this.Row = row;
        }
        static MapUnit()
        {
            MapUnits = new Dictionary<string, MapUnit>();
            //列
            for (int l = 0; l < Map.MapDimensions.X; l++)
            {
                //行
                for (int h = 0; h < Map.MapDimensions.Y; h++)
                {
                    MapUnit unit = new MapUnit(l, h);
                    MapUnits.Add(l + "," + h, unit);
                }
            }
            //装载地形数据
            string[] terrains = MapUnitTerrainType.ParseData();
            for (int i = 0; i < terrains.Length; i++)
            {
                MapUnit unit = MapUnits[((int)(i % Map.MapDimensions.X)) + "," + ((int)(i / Map.MapDimensions.Y))];
                unit.TerrainType = terrains[i];
            }
        }
        //单位地图默认最大最小宽高
        public static int Width = 30;
        public static int Height = 30;
        public static int WidthMax = 100;
        public static int WidthMin = 30;
        public static int HeightMax = 100;
        public static int HeightMin = 30;
        public static int Magnify = 10;//地图单元宽/高放大倍数
        public int Column { set; get; }//列
        public int Row { set; get; }//行
        /// <summary>
        /// 单元的绘制位置数据
        /// </summary>
        public Rectangle DrawPosition=new Rectangle();
        /// <summary>
        /// 单位地形。山地、平原、水域等。用于判断是否显示可以行军的地方
        /// </summary>
        public string TerrainType { get; set; }
        /// <summary>
        /// 地图单元上的城池建筑物
        /// </summary>
        public Building Building { get; set; }

        /// <summary>
        /// 鼠标坐标转地图列行位置
        /// </summary>
        /// <param name="mousePosition"></param>
        /// <returns></returns>
        public static Point MousePositionToMapPosition(Point mousePosition)
        {
            int num = mousePosition.X - GameScreen.LeftEdge;
            int num2 = mousePosition.Y - GameScreen.TopEdge;
            return new Point(num / Width, num2 / Height);
        }
        /// <summary>
        /// 添加地图单元上的建筑物或军队等
        /// </summary>
        /// <param name="building"></param>
        public static void AddThings(Building building)
        {
            string position = building.Position.X + "," + building.Position.Y;
            MapUnit unit = MapUnits[position];
            unit.Building = building;
        }

        public static List<MapUnit> GetDisplayMapUnits()
        {
            _displayMapUnits.Clear();
            for (int i = GameScreen.TopLeftPosition.X; i <= GameScreen.BottomRightPosition.X; i++)
            {
                for (int j = GameScreen.TopLeftPosition.Y; j <= GameScreen.BottomRightPosition.Y; j++)
                {
                    if ((((i >= 0) && (i < Map.MapDimensions.X)) && (j >= 0)) && (j < Map.MapDimensions.Y))
                    {
                        MapUnit unit = MapUnits[i + "," + j];
                        unit.DrawPosition.X = GameScreen.LeftEdge + (unit.Column * Width);
                        unit.DrawPosition.Y = GameScreen.TopEdge + (unit.Row * Height);
                        unit.DrawPosition.Width = Width;
                        unit.DrawPosition.Height = Height;
                        _displayMapUnits.Add(unit);
                    }
                }
            }
            return _displayMapUnits;
        }
    }
}
